package com.bj58.mis.apm.server.statistics.dto.cube;

import com.bj58.mis.apm.server.core.util.JsonUtil;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.util.Arrays;
import java.util.Map;

/**
 * @author: zhangfx
 * @create: 2018-09-28 15:24
 **/
@ApiModel(description = "Cube数据分区设置")
@Data
public class CubePartitionConfig {

    @ApiModelProperty(value = "表名称")
    private String dateTable;

    @ApiModelProperty(value = "分区字段")
    private String dateColumn;

    @ApiModelProperty(value = "日期格式")
    private String dateFormat;


    @ApiModelProperty(value = "表内表名称")
    private String timeTable;

    @ApiModelProperty(value = "表内分区字段")
    private String timeColumn;

    @ApiModelProperty(value = "时间格式")
    private String timeFormat;

    @ApiModelProperty(value = "where条件")
    private String where;




    /*
    {"partition_date_column":"O_M_KYLIN_QUERY_LOG.DT","partition_time_column":"O_M_KYLIN_QUERY_LOG.QUERY_TIME","partition_date_start":0.0,"partition_date_format":"yyyyMM","partition_time_format":"HH","partition_type":"APPEND","partition_condition_builder":"org.apache.kylin.metadata.model.PartitionDesc$DefaultPartitionConditionBuilder"}
     */
    public static CubePartitionConfig fromJson(String jsonStr){
        if(jsonStr==null || jsonStr.isEmpty()){
            return new CubePartitionConfig();
        }
        Map<String, Object> map = JsonUtil.fromJson(jsonStr);

        CubePartitionConfig obj = new CubePartitionConfig();
        String partition_date_column = (String) map.get("partition_date_column");
        if(partition_date_column!=null && partition_date_column.length()>0 && partition_date_column.contains(".")){
            obj.setDateTable(partition_date_column.split("\\.")[0]);
            obj.setDateColumn(partition_date_column.split("\\.")[1]);
        }
        obj.setDateFormat((String) map.get("partition_date_format"));


        String partition_time_column = (String) map.get("partition_time_column");
        if(partition_time_column!=null && partition_time_column.length()>0 && partition_time_column.contains(".")){
            obj.setTimeTable(partition_time_column.split("\\.")[0]);
            obj.setTimeColumn(partition_time_column.split("\\.")[1]);
        }
        obj.setTimeFormat((String) map.get("partition_time_format"));

        return obj;
    }
    public static void main(String[] args){
        System.out.println(Arrays.toString("O_M_KYLIN_QUERY_LOG.DT".split("\\.")));
    }
}
